# GNU make手册：http://www.gnu.org/software/make/manual/make.html
# ************ 遇到不明白的地方请google以及阅读手册 *************

# 编译器设定和编译选项
CC = gcc
FLEX = flex
BISON = bison
CFLAGS = -std=c99

# 编译目标：src目录下的所有.c文件
CFILES = $(shell find ./ -name "*.c")
OBJS = $(CFILES:.c=.o)
LFILE = $(shell find ./ -name "*.l")
YFILE = $(shell find ./ -name "*.y")
LFC = $(shell find ./ -name "*.l" | sed s/[^/]*\\.l/lex.yy.c/)
YFC = $(shell find ./ -name "*.y" | sed s/[^/]*\\.y/syntax.tab.c/)
LFO = $(LFC:.c=.o)
YFO = $(YFC:.c=.o)

parser: syntax $(filter-out $(LFO),$(OBJS))
	$(CC) -o parser $(filter-out $(LFO),$(OBJS)) -lfl -ly

syntax: lexical syntax-c
	$(CC) -c $(YFC) -o $(YFO)

lexical: $(LFILE)
	$(FLEX) -o $(LFC) $(LFILE)

syntax-c: $(YFILE)
	$(BISON) -o $(YFC) -d -v $(YFILE)

-include $(patsubst %.o, %.d, $(OBJS))

# 定义的一些伪目标
.PHONY: gen_ir clean run gen_oc
run: parser
	./parser ../Test/testcase1.cmm ../Test/out1.s
	./parser ../Test/testcase2.cmm ../Test/out2.s
	./parser ../Test/testcase3.cmm ../Test/out3.s
	./parser ../Test/testcase4.cmm ../Test/out4.s

gen_ir: 
	bison -d -t syntax.y 
	flex lexical.l 
	gcc -g -w main_ir.c syntax.tab.c TreeNode.c Semantic.c HashTable.c Gen_ir.c InterCode.c Mips.c -lfl -o ../parser
	../parser ../Test/testcase1.cmm ../Test/out1.ir
	../parser ../Test/testcase2.cmm ../Test/out2.ir
	../parser ../Test/testcase3.cmm ../Test/out3.ir
	../parser ../Test/testcase4.cmm ../Test/out4.ir 
	
gen_oc: 
	bison -d -t syntax.y 
	flex lexical.l 
	gcc -g -w main_oc.c syntax.tab.c TreeNode.c Semantic.c HashTable.c Gen_ir.c InterCode.c Mips.c -lfl -o ../parser
	../parser ../Test/testcase1.cmm ../Test/out1.s
	../parser ../Test/testcase2.cmm ../Test/out2.s
	../parser ../Test/testcase3.cmm ../Test/out3.s
	../parser ../Test/testcase4.cmm ../Test/out4.s 

clean:
#from make gen_*
	rm -f syntax.tab.c syntax.tab.h syntax.output lex.yy.c ../parser
#from make parser
	rm -f *.o parser